package de.tabuma.sso.user;

import de.tabuma.sso.sso.*;
import de.tabuma.sso.useruserrelation.*;
import java.util.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1.2.1 $
 * @since $Date: 2010/01/13 10:23:10 $
 */
public class UserWebserviceLib {
    public static void userHasPrivilegForUser(User user, String ssoId) throws Exception {
        SSO sso = SSOFacade.get(ssoId);
        if(SSOFacade.userHasAdminAll(ssoId)!=-1) {
            return;
        }
        List<UserUserRelation> uurList = UserUserRelationFacade.getList(sso.getUserId());
        for(int i=0;i<uurList.size();i++) {
            List<User> uList = UserFacade.getList1(uurList.get(i).getUser());
            for(int j=0;j<uList.size();j++) {
                if(user.getId().intValue()==uList.get(j).getId().intValue()) {
                    return;
                }
            }
        }
        throw new Exception("User has no privileg to do something with user = "+user.getId()+".");
    }
    public static int userHasPrivileg(String ssoId) throws Exception {
        if(!SSOFacade.sessionIsActiv(ssoId)) {
            throw new Exception("Session has expired.");
        }
        int groupId = SSOFacade.userHasAdminUserPrivileg(ssoId);
        if(groupId==-1) {
            throw new Exception("User has no privileg for this action.");
        }
        return groupId;
    }
}